package com.work.util;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

/**
 * Class used for message provider.
 *
 * @author 880612577
 */
public class MessageProvider implements Serializable{

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The bundle. */
	private ResourceBundle bundle;

	/**
	 * Gets the bundle.
	 *
	 * @return resourceBundle
	 */
	public  ResourceBundle getBundle() {
		if (bundle == null) {
			FacesContext context = FacesContext.getCurrentInstance();
			bundle = ResourceBundle.getBundle("messages",context.getViewRoot().getLocale());//context.getApplication().getResourceBundle(context, "msgs");
		}
		return bundle;
	}

	/**
	 * Retrieve a value for a key.
	 *
	 * @param key key
	 * @return value
	 */
	public String getValue(String key) {

		String result = null;
		try {
			result = getBundle().getString(key);
		} catch (MissingResourceException e) {
			result = "???" + key + "??? not found";
		}
		return result;
	}
	
	/**
	 * Gets the value.
	 *
	 * @param key key
	 * @param params params
	 * @return value
	 */
	public String getValue(String key, Object... params) {
        try {
            return MessageFormat.format(getValue(key), params);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }

}